﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;

namespace DBService
{
	public partial class DlgPM : System.Windows.Forms.Form
	{
		public static DlgPM GetInstance()
		{
			if (DlgPM.dpm == null || DlgPM.dpm.IsDisposed)
			{
				DlgPM.dpm = new DlgPM();
			}
			return DlgPM.dpm;
		}

		public DbUser CurrentUser
		{
			get
			{
				return this.currentUser;
			}
		}

		public DateTime LoginTime
		{
			get
			{
				return this._LoginTime;
			}
			set
			{
				this._LoginTime = value;
			}
		}

		public DlgPM()
		{
			this.InitializeComponent();
		}

		private void button5_Click(object sender, EventArgs e)
		{
		}

		private void btnPM_Click(object sender, EventArgs e)
		{
		}

		private void tsmiRegiste_Click(object sender, EventArgs e)
		{
		}

		private void tsmiFindPW_Click(object sender, EventArgs e)
		{
		}

		private void tsmiChangePW_Click(object sender, EventArgs e)
		{
		}

		private void tsmiLogin_Click(object sender, EventArgs e)
		{
		}

		private void tsmiPM_Click(object sender, EventArgs e)
		{
			new DlgPM().ShowDialog(this);
		}

		private void tsmiRegiste_Click_1(object sender, EventArgs e)
		{
			new DlgRegistrate("PM").ShowDialog(this);
		}

		private void tsmiChangePW_Click_1(object sender, EventArgs e)
		{
			if (this.currentUser != null)
			{
				new DlgChangePassword(this.currentUser.Name).ShowDialog(this);
				return;
			}
			MessageBox.Show("请先登录。");
		}

		private void cmdLogin()
		{
			DlgLogin dlgLogin = new DlgLogin();
			if (dlgLogin.ShowDialog(this) == DialogResult.OK && dlgLogin.User != null)
			{
				if (dlgLogin.User.Status == UserStatus.Unregistered)
				{
					MessageBox.Show("您的账号未通过验证，请联系系统管理员。");
					return;
				}
				dlgLogin.User.IncreaseCount();
				this.currentUser = dlgLogin.User;
				this.tsslWelcom.Text = "";
				this.lbxProject.Items.Clear();
				this.propertyGrid1.SelectedObject = null;
				MessageBox.Show("登录成功！");
				this.tsslWelcom.Text = this.currentUser.ToString() + "你好！这是你第" + this.currentUser.GetCount().ToString() + "次登录。";
				foreach (DbProject item in DbProject.GetFromDB(BimServerDB.Default))
				{
					this.lbxProject.Items.Add(item);
				}
			}
			this.initPermission();
		}

		private void tsmiLogin_Click_1(object sender, EventArgs e)
		{
			if (this.currentUser != null)
			{
				if (MessageBox.Show("当前已登录的账户：" + this.currentUser.Name + "，是否注销并更改用户登录？", "注销", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					this.cmdLogout();
					this.cmdLogin();
					return;
				}
			}
			else
			{
				this.cmdLogin();
			}
		}

		private void tsmiAddRole_Click(object sender, EventArgs e)
		{
			new DlgAddRole(this.mGroup).ShowDialog(this);
		}

		private void tsmiRemove_Click(object sender, EventArgs e)
		{
			new DlgRemoveRole(this.mGroup).ShowDialog(this);
		}

		private void tsmiSetRight_Click(object sender, EventArgs e)
		{
			new DlgSetRight(this.mGroup).ShowDialog(this);
		}

		private void tsmiVerifyUser_Click(object sender, EventArgs e)
		{
			new DlgVerifyUser().ShowDialog(this);
		}

		private void tsmiUnregUser_Click(object sender, EventArgs e)
		{
			new DlgUnregisterUser().ShowDialog(this);
		}

		private void tsmiSetRole_Click(object sender, EventArgs e)
		{
			new DlgSetRole(this.mGroup).ShowDialog(this);
		}

		private void 连接ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgConfig().ShowDialog(this);
		}

		private void 发信息ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgSendMessage(this.currentUser).ShowDialog(this);
		}

		private void 收件箱ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			new DlgMessageBox(this.CurrentUser).ShowDialog(this);
		}

		private void DlgPM_Load(object sender, EventArgs e)
		{
			try
			{
				this.autoLogin();
				this.initPermission();
				if (this.currentUser == null)
				{
					this.cmdLogin();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				base.Close();
			}
		}

		private void autoLogin()
		{
			try
			{
				string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
				DbUser user = DbUser.GetUser(XmlUtils.GetDataFromConfigFile(fileName, "AutoUser", "value"), BimServerDB.Default);
				if (user != null)
				{
					string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(fileName, "AutoPwd", "value");
					if (user.Password.Equals(DbUser.ToMd5(dataFromConfigFile)))
					{
						this.currentUser = user;
						this.tsslWelcom.Text = "";
						this.lbxProject.Items.Clear();
						this.propertyGrid1.SelectedObject = null;
						this.tsslWelcom.Text = this.currentUser.ToString() + "你好！这是你第" + this.currentUser.GetCount().ToString() + "次登录。";
						foreach (DbProject item in DbProject.GetFromDB(BimServerDB.Default))
						{
							this.lbxProject.Items.Add(item);
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			this.cmdAddProject();
		}

		private void cmdAddProject()
		{
			new DlgAddProject().ShowDialog(this);
			this.lbxProject.Items.Clear();
			foreach (DbProject item in DbProject.GetFromDB(BimServerDB.Default))
			{
				this.lbxProject.Items.Add(item);
			}
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			this.cmdDelProject();
		}

		private void cmdDelProject()
		{
			if (this.lbxProject.SelectedItem != null && MessageBox.Show("是否需要删除选定项目？", "项目管理", MessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				try
				{
					((DbProject)this.lbxProject.SelectedItem).RemoveFromDb();
					this.lbxProject.Items.Remove(this.lbxProject.SelectedItem);
					this.propertyGrid1.SelectedObject = null;
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lbxProject.SelectedItem != null)
			{
				this.propertyGrid1.SelectedObject = (DbProject)this.lbxProject.SelectedItem;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.cmdUpdate();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			this.cmdDetails();
		}

		private void cmdDetails()
		{
			if (this.lbxProject.SelectedItem != null && this.lbxProject.SelectedItem != null)
			{
				new DlgProjectView((DbProject)this.lbxProject.SelectedItem).ShowDialog(this);
			}
		}

		private void tsmiCreate_Click(object sender, EventArgs e)
		{
			this.cmdAddProject();
		}

		private void tsmiDel_Click(object sender, EventArgs e)
		{
			this.cmdDelProject();
		}

		private void tsmiUpdate_Click(object sender, EventArgs e)
		{
			this.cmdUpdate();
		}

		private void cmdApproval()
		{
			if (this.lbxProject.SelectedItem == null)
			{
				MessageBox.Show("请先选择项目。");
				return;
			}
			new DlgProjectUser((DbProject)this.lbxProject.SelectedItem, this.currentUser).ShowDialog(this);
		}

		private void cmdUpdate()
		{
			if (this.lbxProject.SelectedItem != null)
			{
				if (((DbProject)this.propertyGrid1.SelectedObject).Name == "")
				{
					MessageBox.Show("项目名不能为空！");
					return;
				}
				((DbProject)this.propertyGrid1.SelectedObject).UpdateToDb();
				this.lbxProject.Items.Clear();
				foreach (DbProject item in DbProject.GetFromDB(BimServerDB.Default))
				{
					this.lbxProject.Items.Add(item);
				}
			}
		}

		private void tsmiLogout_Click(object sender, EventArgs e)
		{
			this.cmdLogout();
		}

		private void cmdLogout()
		{
			this.currentUser = null;
			this.tsslWelcom.Text = "";
			this.lbxProject.Items.Clear();
			this.propertyGrid1.SelectedObject = null;
			string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
			XmlUtils.WriteConfigFile(fileName, "AutoUser", "value", "");
			XmlUtils.WriteConfigFile(fileName, "AutoPwd", "value", "");
			this.initPermission();
		}

		private void initPermission()
		{
			this.btnCreate.Enabled = false;
			this.btnEdit.Enabled = false;
			this.btnDel.Enabled = false;
			this.tsmiCreate.Enabled = false;
			this.tsmiDel.Enabled = false;
			this.tsmiUpdate.Enabled = false;
			this.tsmiApproval.Enabled = false;
			this.tsmiInbox.Enabled = false;
			this.tsmiSent.Enabled = false;
			this.tsmiAdmin.Visible = false;
			this.tsmiApproval.Enabled = false;
			if (this.currentUser != null)
			{
				this.tsmiInbox.Enabled = true;
				this.tsmiSent.Enabled = true;
				foreach (DbRight dbRight in this.currentUser.GetRightList())
				{
					if (dbRight.Name.Trim() == "创建项目")
					{
						this.btnCreate.Enabled = true;
						this.tsmiCreate.Enabled = true;
					}
					if (dbRight.Name.Trim() == "删除项目")
					{
						this.btnDel.Enabled = true;
						this.tsmiDel.Enabled = true;
					}
					if (dbRight.Name.Trim() == "修改项目")
					{
						this.btnEdit.Enabled = true;
						this.tsmiUpdate.Enabled = true;
					}
					if (dbRight.Name.Trim() == "加入项目")
					{
						this.tsmiApproval.Enabled = true;
					}
					if (dbRight.Name.Trim() == "项目管理")
					{
						this.tsmiAdmin.Visible = true;
					}
				}
			}
		}

		private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
		{
		}

		private void tsmiDelUser_Click(object sender, EventArgs e)
		{
			new DlgRemoveUser().ShowDialog(this);
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			try
			{
				if (this.currentUser != null)
				{
					if (this.currentUser.NewMessageCount > 0)
					{
						this.tsmiCommunication.Text = "通讯(" + this.currentUser.NewMessageCount + ")";
						this.tsmiInbox.Text = "收件箱(" + this.currentUser.NewMessageCount + ")";
					}
					else
					{
						this.tsmiCommunication.Text = "通讯";
						this.tsmiInbox.Text = "收件箱";
					}
					this.initPermission();
				}
			}
			catch (Exception)
			{
				this.timer1.Enabled = false;
			}
		}

		private void tsmiApproval_Click(object sender, EventArgs e)
		{
			this.cmdApproval();
		}

		private void tsmiUPApproval_Click(object sender, EventArgs e)
		{
			new DlgPorjectUserApproval().ShowDialog(this);
		}

		private void 审批操作ToolStripMenuItem_Click(object sender, EventArgs e)
		{
		}

		private void DlgPM_FormClosed(object sender, FormClosedEventArgs e)
		{
			base.Dispose();
		}

		private static DlgPM dpm;

		private string mGroup = "PM";

		private DbUser currentUser;

		private DateTime _LoginTime;
	}
}
